package com.sys.utils;

import com.sys.entity.TxtExport;

import java.io.*;
import java.util.Date;

/**
 * @Author com.bhudy
 * @Description 文件管理，代码生成工具类
 * @Date 2019-10-30 23:09:37
 */
public final class TxtExportUtils {

    private TxtExportUtils() {
    }

    /**
     * 创建Entity
     */
    public static boolean createEntity(TxtExport txtExport) throws IOException {
        String path = txtExport.getPath() + "entity\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();
        StringBuffer sbGetAndSet = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + ".java";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("package " + txtExport.getPackagePath() + ".entity;\n\n");

            for (String type : txtExport.getTypes()) {
                if (type.equals("Date")) {
                    sb.append("import java.util.Date;\n");
                    break;
                }
            }

            for (String key : txtExport.getKeys()) {
                if (!key.equals("") && !key.equals("@PrimaryKey")) {
                    sb.append("import com.bhudy.annotation.ManyToOne;\n");
                    break;
                }
            }

            sb.append("import com.bhudy.annotation.PrimaryKey;\n\n");

            sb.append("/**\n");
            sb.append(" * @Author com.bhudy\n");
            sb.append(" * @Description " + txtExport.getClassName() + "实体类\n");
            sb.append(" * @Date " + new Date() + "\n");
            sb.append(" */\n");
            sb.append("public class " + txtExport.getClassName() + " {\n\n");

            for (int i = 0; i < txtExport.getColumns().size(); i++) {
                if (txtExport.getKeys().get(i).equals("") || txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                    if (txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                        sb.append("    @PrimaryKey\n");
                    }
                    sb.append("    private " + txtExport.getTypes().get(i) + " " + txtExport.getColumns().get(i) + ";    //" + txtExport.getComments().get(i) + "\n");

                    sbGetAndSet.append("    public " + txtExport.getTypes().get(i) + " get" + Utils.toUpperCaseFirstOneDel(txtExport.getColumns().get(i)) + "() {\n");
                    sbGetAndSet.append("        return " + txtExport.getColumns().get(i) + ";\n");
                    sbGetAndSet.append("    }\n\n");

                    sbGetAndSet.append("    public void set" + Utils.toUpperCaseFirstOneDel(txtExport.getColumns().get(i)) + "(" + txtExport.getTypes().get(i) + " " + txtExport.getColumns().get(i) + ") {\n");
                    sbGetAndSet.append("        this." + txtExport.getColumns().get(i) + " = " + txtExport.getColumns().get(i) + ";\n ");
                    sbGetAndSet.append("    }\n\n");
                } else if (!txtExport.getKeys().get(i).equals("@PrimaryKey") && !txtExport.getKeys().get(i).equals("")) {
                    sb.append("    private " + txtExport.getTypes().get(i) + " " + txtExport.getColumns().get(i) + ";    //" + txtExport.getComments().get(i) + "\n");

                    sbGetAndSet.append("    public " + txtExport.getTypes().get(i) + " get" + Utils.toUpperCaseFirstOneDel(txtExport.getColumns().get(i)) + "() {\n");
                    sbGetAndSet.append("        return " + txtExport.getColumns().get(i) + ";\n");
                    sbGetAndSet.append("    }\n\n");

                    sbGetAndSet.append("    public void set" + Utils.toUpperCaseFirstOneDel(txtExport.getColumns().get(i)) + "(" + txtExport.getTypes().get(i) + " " + txtExport.getColumns().get(i) + ") {\n");
                    sbGetAndSet.append("        this." + txtExport.getColumns().get(i) + " = " + txtExport.getColumns().get(i) + ";\n ");
                    sbGetAndSet.append("    }\n\n");


                    sb.append("    @ManyToOne(\"" + txtExport.getColumns().get(i) + "\")\n");
                    sb.append("    private " + txtExport.getKeys().get(i) + " " + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + ";    //" + txtExport.getComments().get(i) + "\n");

                    sbGetAndSet.append("    public " + txtExport.getKeys().get(i) + " get" + Utils.toUpperCaseFirstOneDel(txtExport.getKeys().get(i)) + "() {\n");
                    sbGetAndSet.append("        return " + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + ";\n");
                    sbGetAndSet.append("    }\n\n");

                    sbGetAndSet.append("    public void set" + Utils.toUpperCaseFirstOneDel(txtExport.getKeys().get(i)) + "(" + txtExport.getKeys().get(i) + " " + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + ") {\n");
                    sbGetAndSet.append("        this." + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + " = " + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + ";\n ");
                    sbGetAndSet.append("    }\n\n");
                }
            }
            sb.append("\n");

            sb.append(sbGetAndSet);

            sb.append("}\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }


    /**
     * 创建Dao
     */
    public static boolean createDao(TxtExport txtExport) throws IOException {
        String path = txtExport.getPath() + "dao\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + "Dao.java";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("package " + txtExport.getPackagePath() + ".dao;\n\n");

            sb.append("import org.springframework.stereotype.Repository;\n\n");

            sb.append("/**\n");
            sb.append(" * @Author com.bhudy\n");
            sb.append(" * @Description " + txtExport.getClassName() + "Dao\n");
            sb.append(" * @Date " + new Date() + "\n");
            sb.append(" */\n");
            sb.append("@Repository\n");
            sb.append("public interface " + txtExport.getClassName() + "Dao {\n\n");


            sb.append("}\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }


    /**
     * 创建mapper
     */
    public static boolean createMapper(TxtExport txtExport) throws IOException {
        String path = System.getProperty("user.dir") + "\\src\\main\\resources\\mapping\\" + txtExport.getClass2Path() + "\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + "Mapper.xml";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n");
            sb.append("<mapper namespace=\"" + txtExport.getPackagePath() + ".dao." + txtExport.getClassName() + "Dao\">\n");
            sb.append("    <resultMap id=\"BaseResultMap\" type=\"" + txtExport.getClassName() + "\">\n");

            StringBuffer sbu = new StringBuffer();
            for (int i = 0; i < txtExport.getFields().size(); i++) {
                if (i == 0) {
                    sb.append("        <id column=\"" + txtExport.getFields().get(i) + "\" jdbcType=\"" + txtExport.getJdbcTypes().get(i) + "\" property=\"" + txtExport.getColumns().get(i) + "\" />\n");
                } else if (!txtExport.getKeys().get(i).equals("")) {
                    sbu.append("        <association property=\"" + Utils.toLowerCaseFirstOneDel(txtExport.getKeys().get(i)) + "\" javaType=\"" + txtExport.getKeys().get(i) + "\" resultMap=\"" + txtExport.getPackagePath() + ".dao." + txtExport.getKeys().get(i) + "Dao.BaseResultMap\"/>\n");
                } else {
                    sb.append("        <result column=\"" + txtExport.getFields().get(i) + "\" jdbcType=\"" + txtExport.getJdbcTypes().get(i) + "\" property=\"" + txtExport.getColumns().get(i) + "\" />\n");
                }
            }
            sb.append("    </resultMap>\n\n");

            if (sbu.length() > 0) {
                sb.append("    <resultMap id=\"FullResultMap\" type=\"" + txtExport.getClassName() + "\" extends=\"BaseResultMap\">\n");
                sb.append(sbu);
                sb.append("    </resultMap>\n\n");
            }
            sb.append("    <!-- @Author com.bhudy\n");
            sb.append("    @Description " + txtExport.getClassName() + "Dao\n");
            sb.append("    @Date " + new Date() + " -->\n\n");

            sb.append("</mapper>\n\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }

    /**
     * 创建Service
     */
    public static boolean createService(TxtExport txtExport) throws IOException {
        String path = txtExport.getPath() + "service\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + "Service.java";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("package " + txtExport.getPackagePath() + ".service;\n\n");

            sb.append("import com.bhudy.base.BaseService;\n");
            sb.append("import " + txtExport.getPackagePath() + ".entity." + txtExport.getClassName() + ";\n\n");

            sb.append("import java.io.Serializable;\n\n");

            sb.append("/**\n");
            sb.append(" * @Author com.bhudy\n");
            sb.append(" * @Description " + txtExport.getClassName() + "Service\n");
            sb.append(" * @Date " + new Date() + "\n");
            sb.append(" */\n");
            sb.append("public interface " + txtExport.getClassName() + "Service extends BaseService<" + txtExport.getClassName() + ", Serializable> {\n\n");

            sb.append("}\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }

    /**
     * 创建ServiceImpl
     */
    public static boolean createServiceImpl(TxtExport txtExport) throws IOException {
        String path = txtExport.getPath() + "service\\impl\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + "ServiceImpl.java";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("package " + txtExport.getPackagePath() + ".service.impl;\n\n");


            sb.append("import com.bhudy.base.BaseServiceImpl;\n");
            sb.append("import " + txtExport.getPackagePath() + ".entity." + txtExport.getClassName() + ";\n");
            sb.append("import " + txtExport.getPackagePath() + ".service." + txtExport.getClassName() + "Service;\n");
            sb.append("import org.springframework.stereotype.Service;\n\n");

            sb.append("import java.io.Serializable;\n\n");

            sb.append("/**\n");
            sb.append(" * @Author com.bhudy\n");
            sb.append(" * @Description " + txtExport.getClassName() + "ServiceImpl\n");
            sb.append(" * @Date " + new Date() + "\n");
            sb.append(" */\n");
            sb.append("@Service\n");
            sb.append("public class " + txtExport.getClassName() + "ServiceImpl extends BaseServiceImpl<" + txtExport.getClassName() + ", Serializable> implements " + txtExport.getClassName() + "Service {\n\n");

            sb.append("}\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }


    /**
     * 创建Controller
     */
    public static boolean createController(TxtExport txtExport) throws IOException {
        String path = txtExport.getPath() + "controller\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += txtExport.getClassName() + "Controller.java";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }

        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            sb.append("package " + txtExport.getPackagePath() + ".controller;\n\n");

            sb.append("import com.alibaba.fastjson.JSONObject;\n");
            sb.append("import " + txtExport.getPackagePath() + ".entity." + txtExport.getClassName() + ";\n");
            sb.append("import com.bhudy.utils.ResponseResult;\n");
            sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
            sb.append("import org.springframework.web.bind.annotation.*;\n");
            sb.append("import com.bhudy.base.QueryFilter;\n");

            sb.append("import " + txtExport.getPackagePath() + ".service." + txtExport.getClassName() + "Service;\n\n");

            sb.append("/**\n");
            sb.append(" * @Author com.bhudy\n");
            sb.append(" * @Description " + txtExport.getClassName() + "Controller\n");
            sb.append(" * @Date " + new Date() + "\n");
            sb.append(" */\n");
            sb.append("@RestController\n");
            sb.append("public class " + txtExport.getClassName() + "Controller {\n\n");

            sb.append("    @Autowired\n");
            sb.append("    private " + txtExport.getClassName() + "Service " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service;\n");

            if (txtExport.getSelect() != null && txtExport.getSelect()) {
                sb.append("    /**\n");
                sb.append("     * 获取" + txtExport.getClassName() + "表的数据\n");
                sb.append("     *\n");
                sb.append("     * @param page          页数{1}\n");
                sb.append("     * @param limit         显示数量{10}\n");
                sb.append("     * @return " + txtExport.getClassName() + "的list\n");
                sb.append("     */\n");
                sb.append("    @GetMapping(value = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "s\")\n");
                sb.append("    public ResponseResult getAll(Integer page, Integer limit,");
                StringBuffer selSb = new StringBuffer("        QueryFilter queryFilter = new QueryFilter();\n");
                selSb.append("        if (page != null && limit != null) queryFilter.limit(--page, limit);\n");
                int num = 0;
                for (int i = 0; i < txtExport.getSelectList().size(); i++) {
                    String select = txtExport.getSelectList().get(i);
                    String column = txtExport.getColumns().get(i);
                    String type = txtExport.getTypes().get(i);
                    if (select != null && !select.equals("")) {
                        sb.append(type + " " + column + ",");
                        selSb.append("        if(" + column + " != null && !" + column + ".toString().equals(\"\")) {\n");
                        if (select.equals("andLike") || select.equals("orLike")) {
                            selSb.append("            queryFilter." + select + "(\"" + column + "\", \"%\"+" + column + "+\"%\");\n");
                        } else {
                            selSb.append("            queryFilter." + select + "(\"" + column + "\", " + column + ");\n");
                        }
                        selSb.append("        }\n");
                        num++;
                    }
                }
                /*if (num != 0) sb.deleteCharAt(sb.length() - 1);*/
                sb.deleteCharAt(sb.length() - 1);
                sb.append("){\n");
                sb.append(selSb);
                sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.findList(queryFilter));\n");
                sb.append("    }\n\n");

                sb.append("    @GetMapping(value = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\")\n");
                boolean is = true;
                for (int i = 0; i < txtExport.getColumns().size(); i++) {
                    if (txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                        sb.append("    public ResponseResult get(" + txtExport.getTypes().get(i) + " " + txtExport.getColumns().get(i) + ") {\n");
                        sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.get(" + txtExport.getColumns().get(i) + "));\n");
                        is = false;
                        break;
                    }
                }
                if (is) {
                    sb.append("    public ResponseResult get(Long id) {\n");
                    sb.append("        return ResponseResult.printMapJson(" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.get(id));\n");
                }
                sb.append("    }\n\n");
            }

            if (txtExport.getInsert() != null && txtExport.getInsert()) {
                sb.append("    @PostMapping(value = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\")\n");
                sb.append("    public ResponseResult insert(@RequestBody String json) {\n");
                sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.insert(JSONObject.parseObject(json, " + txtExport.getClassName() + ".class)));\n");
                sb.append("    }\n\n");
            }

            if (txtExport.getUpdate() != null && txtExport.getUpdate()) {
                sb.append("    @PutMapping(value = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\")\n");
                sb.append("    public ResponseResult update(@RequestBody String json) {\n");
                sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.update(JSONObject.parseObject(json, " + txtExport.getClassName() + ".class)));\n");
                sb.append("    }\n\n");
            }

            if (txtExport.getDelete() != null && txtExport.getDelete()) {
                sb.append("    @DeleteMapping(value = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "s\")\n");
                sb.append("    public ResponseResult delete(@RequestBody String json) {\n");
                boolean is = true;
                for (int i = 0; i < txtExport.getColumns().size(); i++) {
                    if (txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                        sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.delete(JSONObject.parseObject(json, " + txtExport.getTypes().get(i) + "[].class)));\n");
                        is = false;
                        break;
                    }
                }
                if (is) {
                    sb.append("        return ResponseResult.printMapJson(" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.delete(JSONObject.parseObject(json, Long[].class)));\n");
                }
                sb.append("    }\n\n");
            }

            /*if (txtExport.getInsert() != null && txtExport.getInsert().equals("on")) {
                sb.append("    @PostMapping(value = \"/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\")\n");
                sb.append("    public ResponseResult insert(" + txtExport.getClassName() + " " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "){\n");
                sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.insert(" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "));\n");
                sb.append("    }\n\n");
            }

            if (txtExport.getUpdate() != null && txtExport.getUpdate().equals("on")) {
                sb.append("    @PutMapping(value = \"/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\")\n");
                sb.append("    public ResponseResult update(" + txtExport.getClassName() + " " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "){\n");
                sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.update(" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "));\n");
                sb.append("    }\n\n");
            }

            if (txtExport.getDelete() != null && txtExport.getDelete().equals("on")) {
                sb.append("    @DeleteMapping(value = \"/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "s\")\n");
                boolean is = true;
                for (int i = 0; i < txtExport.getColumns().size(); i++) {
                    if (txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                        sb.append("    public ResponseResult delete(" + txtExport.getTypes().get(i) + "[] " + txtExport.getColumns().get(i) + ") {\n");
                        sb.append("        return ResponseResult.printMapJson(0, " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.delete(" + txtExport.getColumns().get(i) + "));\n");
                        is = false;
                        break;
                    }
                }
                if (is) {
                    sb.append("    public ResponseResult delete(String[] id) {\n");
                    sb.append("        return ResponseResult.printMapJson(" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "Service.delete(id));\n");
                }
                sb.append("    }\n\n");
            }*/

            sb.append("}\n");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }


    /**
     * 创建Template
     */
    public static boolean createTemplate(TxtExport txtExport) throws IOException {
        String path = System.getProperty("user.dir") + "\\src\\main\\resources\\templates\\" + txtExport.getClass2Path() + "\\";
        File file = new File(path);
        FileOutputStream fos = null;
        PrintWriter pw = null;
        StringBuffer sb = new StringBuffer();

        if (!file.exists()) {
            file.mkdirs();
        }

        path += Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + ".html";
        File filename = new File(path);

        if (filename.exists()) {
            System.out.println("文件存在");
            return false;
        }


        try {
            filename.createNewFile();
            fos = new FileOutputStream(filename);
            pw = new PrintWriter(fos);

            boolean isSelect = false;
            String key = "";

            sb.append("<!--\n");
            sb.append("@Author com.bhudy\n");
            sb.append("@Description " + txtExport.getClassName() + "\n");
            sb.append("@Date " + new Date() + "\n");
            sb.append("-->\n\n");
            sb.append("<!DOCTYPE html>\n");
            sb.append("<html lang=\"en\">\n");
            sb.append("<head>\n");
            sb.append("    <meta charset=\"UTF-8\">\n");
            sb.append("    <meta name=\"viewport\"\n");
            sb.append("          content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n");
            sb.append("    <title>Sys - " + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "</title>\n");
            sb.append("    <script src=\"/res/js/sys.js\"></script>\n");
            sb.append("    <script src=\"/res/layui/layui.js\"></script>\n");
            sb.append("</head>\n");
            sb.append("<body>\n");
            sb.append("<div class=\"layui-fluid\">\n");
            sb.append("    <div class=\"layui-row\">\n");
            sb.append("        <div class=\"layui-col-xs12\">\n");
            sb.append("            <form class=\"layui-form\" action=\"\">\n");
            sb.append("                <div class=\"layui-form-item\">\n");

            for (int i = 0; i < txtExport.getSelectList().size(); i++) {
                String select = txtExport.getSelectList().get(i);
                String column = txtExport.getColumns().get(i);
                if (select != null && !select.equals("")) {
                    sb.append("                    <label class=\"layui-form-label\">" + column + "</label>\n");
                    sb.append("                    <div class=\"layui-input-inline\">\n");
                    sb.append("                        <input type=\"text\" name=\"" + column + "\" lay-verify=\"" + column + "\" autocomplete=\"off\" placeholder=\"请输入" + column + "\"\n");
                    sb.append("                               class=\"layui-input\">\n");
                    sb.append("                    </div>\n");
                    isSelect = true;
                }
            }
            sb.append("                    <div class=\"layui-inline\">\n");
            if (isSelect) {
                sb.append("                        <button type=\"submit\" class=\"layui-btn layui-btn-normal\" lay-submit=\"\" lay-filter=\"search\">搜索</button>\n");
                sb.append("                        <button type=\"reset\" class=\"layui-btn layui-btn-primary\">重置</button>\n");
            }
            sb.append("                        <button type=\"button\" class=\"layui-btn layui-btn-primary\" onclick=\"javascript:location.reload();\">刷新</button>");
            sb.append("                        </div>\n");
            sb.append("                    </div>\n");
            sb.append("            </form>\n");
            sb.append("            <table id=\"tableDataList\" class=\"layui-table\" lay-filter=\"tableDataList\"\n");
            sb.append("                   lay-data=\"{url:'/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "s', page:true, id:'tableDataList',toolbar:'default', defaultToolbar:['filter', 'exports', 'print']}\">\n");
            sb.append("                <thead>\n");
            sb.append("                <tr>\n");
            sb.append("                    <th lay-data=\"{type:'checkbox', fixed: 'left'}\"></th>\n");
            for (int i = 0; i < txtExport.getColumns().size(); i++) {
                if (txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                    sb.append("                    <th lay-data=\"{field:'" + txtExport.getColumns().get(i) + "', width:80, sort: true, fixed: 'left'}\">ID</th>\n");
                    key = txtExport.getColumns().get(i);
                    break;
                }
            }
            for (int i = 0; i < txtExport.getColumns().size(); i++) {
                if (txtExport.getKeys().get(i).equals("") && !txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                    sb.append("                    <th lay-data=\"{field: '" + txtExport.getColumns().get(i) + "', width:150}\">" + txtExport.getColumns().get(i) + "</th>\n");
                }
            }
            sb.append("                    <th lay-data=\"{fixed: 'right', width:178, align:'center', toolbar: '#bar'}\"></th>\n");
            sb.append("                </tr>\n");
            sb.append("                </thead>\n");
            sb.append("            </table>\n");
            sb.append("\n");
            sb.append("            <script type=\"text/html\" id=\"bar\">\n");
            sb.append("                <a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\">编辑</a>\n");
            sb.append("                <a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a>\n");
            sb.append("            </script>\n");
            sb.append("        </div>\n");
            sb.append("    </div>\n");
            sb.append("</div>\n");
            sb.append("</body>\n");
            sb.append("<script>\n");
            sb.append("    layui.use(['table', 'form', 'layedit', 'laydate'], function () {\n");
            sb.append("        var table = layui.table,\n");
            sb.append("            form = layui.form,\n");
            sb.append("            layer = layui.layer,\n");
            sb.append("            layedit = layui.layedit,\n");
            sb.append("            laydate = layui.laydate,\n");
            sb.append("            url = \"/api/v1/" + Utils.toLowerCaseFirstOneDel(txtExport.getClassName()) + "\",\n");
            sb.append("            $ = layui.jquery;\n");
            sb.append("\n");
            sb.append("\n");
            sb.append("        //监听头工具栏事件\n");
            sb.append("        table.on('toolbar(tableDataList)', function (obj) {\n");
            sb.append("            var checkStatus = table.checkStatus(obj.config.id)\n");
            sb.append("                , dataList = checkStatus.data; //获取选中的数据\n");
            sb.append("            switch (obj.event) {\n");
            sb.append("                case 'add':\n");
            sb.append("                    showUpdateOrCreate();\n");
            sb.append("                    break;\n");
            sb.append("                case 'update':\n");
            sb.append("                    if (dataList.length === 0) {\n");
            sb.append("                        layer.msg('请选择一行');\n");
            sb.append("                    } else if (dataList.length > 1) {\n");
            sb.append("                        layer.msg('只能同时编辑一个');\n");
            sb.append("                    } else {\n");
            sb.append("                        showUpdateOrCreate(dataList[0]);\n");
            sb.append("                    }\n");
            sb.append("                    break;\n");
            sb.append("                case 'delete':\n");
            sb.append("                    if (dataList.length === 0) {\n");
            sb.append("                        layer.msg('请选择一行');\n");
            sb.append("                    } else {\n");
            sb.append("                        var " + key + "s = \"\";\n");
            sb.append("                        dataList.forEach(data => {\n");
            sb.append("                            " + key + "s += \",\" + data." + key + ";");
            sb.append("                        });\n");
            sb.append("                        delFun(" + key + "s);\n");
            sb.append("                    }\n");
            sb.append("                    break;\n");
            sb.append("            }\n");
            sb.append("        });\n");
            sb.append("\n");
            sb.append("        //监听行工具事件\n");
            sb.append("        table.on('tool(tableDataList)', function (obj) {\n");
            sb.append("            var data = obj.data;\n");
            sb.append("            if (obj.event === 'del') {\n");
            sb.append("                 delFun([data." + key + "]);");
            sb.append("            } else if (obj.event === 'edit') {\n");
            sb.append("                showUpdateOrCreate(data)\n");
            sb.append("            }\n");
            sb.append("        });\n");
            sb.append("\n");
            sb.append("        // 搜索框\n");
            sb.append("        form.on('submit(search)', function (data) {\n");
            sb.append("            table.reload(\"tableDataList\", {\n");
            sb.append("                where: data.field,\n");
            sb.append("                page: {curr: 1}//重新从第 1 页开始\n");
            sb.append("            });\n");
            sb.append("            return false;\n");
            sb.append("        });\n");
            sb.append("\n");
            sb.append("        // 修改或者添加方法\n");
            sb.append("        form.on('submit(updateOrCreateBtn)', function (data) {\n");
            sb.append("            if (data.field." + key + ") {\n");
            sb.append("                $.ajaxJsonPUT(url, JSON.stringify(data.field), function (resultData) {\n");
            sb.append("                    resultFun(resultData);\n");
            sb.append("                })\n");
            sb.append("            } else {\n");
            sb.append("                $.ajaxJsonPOST(url, JSON.stringify(data.field), function (resultData) {\n");
            sb.append("                    resultFun(resultData);\n");
            sb.append("                })\n");
            sb.append("            }\n");
            sb.append("            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。\n");
            sb.append("        });\n");
            sb.append("\n");
            sb.append("\n");
            sb.append("        // 删除方法\n");
            sb.append("        function delFun(ids) {\n");
            sb.append("            layer.confirm('真的删除行么', function (index) {\n");
            sb.append("                $.ajaxJsonDEL(url + \"s\", JSON.stringify(ids), function (resultData) {\n");
            sb.append("                    layer.msg(resultData.message);\n");
            sb.append("                    setTimeout(function () {\n");
            sb.append("                        layer.closeAll();\n");
            sb.append("                        table.reload(\"tableDataList\", {\n");
            sb.append("                            where: null,\n");
            sb.append("                            page: {curr: 1}//重新从第 1 页开始\n");
            sb.append("                        });\n");
            sb.append("                    }, 2000);\n");
            sb.append("                });\n");
            sb.append("            });\n");
            sb.append("        }");
            sb.append("\n");
            sb.append("\n");
            sb.append("        // 拿到后台返回结果做处理\n");
            sb.append("        function resultFun(resultData) {\n");
            sb.append("            layer.msg(resultData.message);\n");
            sb.append("            if (resultData.code === 0) {\n");
            sb.append("                setTimeout(function () {\n");
            sb.append("                    layer.closeAll();\n");
            sb.append("                    table.reload(\"tableDataList\", {\n");
            sb.append("                        where: null,\n");
            sb.append("                        page: {curr: 1}//重新从第 1 页开始\n");
            sb.append("                    });\n");
            sb.append("                }, 2000);\n");
            sb.append("            }\n");
            sb.append("        }\n");
            sb.append("\n");
            sb.append("        // 显示修改或者添加弹出窗口\n");
            sb.append("        function showUpdateOrCreate(data) {\n");
            sb.append("            //页面层-自定义\n");
            sb.append("            let index = layer.open({\n");
            sb.append("                type: 1,\n");
            sb.append("                title: '操作窗口',\n");
            sb.append("                closeBtn: 1,\n");
            sb.append("                skin: 'layui-layer-lan',\n");
            sb.append("                maxmin: true,\n");
            sb.append("                btn: ['提交', '取消'],\n");
            sb.append("               yes: function (index, layero) {\n");
            sb.append("                    $(\"#updateOrCreateBtn\").click();\n");
            sb.append("                    return false;\n");
            sb.append("                },\n");
            sb.append("                content: `<form class=\"layui-form\" style=\"margin: 10px 50px;\" action=\"\" lay-filter=\"updateOrCreateForm\">\n");
            sb.append("                            <input type=\"hidden\" name=\"" + key + "\" lay-verify=\"" + key + "\" autocomplete=\"off\" class=\"layui-input\">\n");

            for (int i = 0; i < txtExport.getColumns().size(); i++) {
                if (txtExport.getKeys().get(i).equals("") && !txtExport.getKeys().get(i).equals("@PrimaryKey")) {
                    sb.append("                            <div class=\"layui-form-item\">\n");
                    sb.append("                                <label class=\"layui-form-label\">" + txtExport.getColumns().get(i) + "</label>\n");
                    sb.append("                                <div class=\"layui-input-block\">\n");
                    sb.append("                                    <input type=\"text\" name=\"" + txtExport.getColumns().get(i) + "\" autocomplete=\"off\" class=\"layui-input\">\n");
                    sb.append("                                </div>\n");
                    sb.append("                            </div>\n");
                }
            }
            sb.append("                            <button type=\"submit\" style=\"display: none;\" id=\"updateOrCreateBtn\" class=\"layui-btn layui-btn-normal\" lay-submit=\"\" lay-filter=\"updateOrCreateBtn\"></button>\n");
            sb.append("                        </form>`\n");
            sb.append("            });\n");
            sb.append("            layer.full(index);\n");
            sb.append("            // 绑定数据\n");
            sb.append("            form.val('updateOrCreateForm', data);\n");
            sb.append("        }\n");
            sb.append("    });\n");
            sb.append("</script>\n");
            sb.append("</html>");

            pw.write(sb.toString());
            pw.flush();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (pw != null) pw.close();
            if (fos != null) fos.close();
        }

        return true;
    }

}
